package org.nfIt.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.nfIt.entity.product.Category;
import org.nfIt.entity.product.ProductSku;
import org.nfIt.service.CategoryService;
import org.nfIt.service.ProductService;
import org.nfIt.vo.common.Result;
import org.nfIt.vo.common.ResultCodeEnum;
import org.nfIt.vo.h5.IndexVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * ClossName:IndexController
 * Pakage:org.nfIt.controller
 * Description
 *
 * h5首页
 *
 * @Author L
 * @Create 2024/9/18 20:17
 * @Version 2023.1.3
 */
@Tag(name = "h5首页接口管理")
@RestController
@RequestMapping(value="/api/product/index")
@SuppressWarnings({"unchecked", "rawtypes"})
//可以解决跨域问题（临时的）
//@CrossOrigin
public class IndexController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private ProductService productService;

    /**
     * 查询一级分类和热销商品数据
     * @return
     */
    @Operation(summary = "获取首页数据")
    @GetMapping
    public Result<IndexVo> findData(){
        //查询一级分类
        List<Category> categoryList = categoryService.findOneCategory();
        //查询热销商品，根据sale_num查询前10条
        List<ProductSku> productSkuList = productService.findProductSkuBySale();

        //把数据封装在vo里
        IndexVo indexVo = new IndexVo() ;
        indexVo.setCategoryList(categoryList);
        indexVo.setProductSkuList(productSkuList);

        return Result.build(indexVo , ResultCodeEnum.SUCCESS);
    }

}
